package com.huluobo.a2203kotlinshopping.detail

import androidx.lifecycle.MutableLiveData
import com.huluobo.base.bean.AddToCartBeanResult
import com.huluobo.base.bean.DetailBeanResult
import com.huluobo.base.model.BaseRepo
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import okhttp3.RequestBody

/**
 *  Created by LC on 2024/6/26.
 */
class DetailRepo : BaseRepo() {
    fun getGoodsDetail(goodsId: Int, success: MutableLiveData<DetailBeanResult>, failed: MutableLiveData<String>) {
        apiService.getGoodsDetail(goodsId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<DetailBeanResult> {
                override fun onSubscribe(d: Disposable) {
                }

                override fun onError(e: Throwable) {
                    failed.value = e.message
                }

                override fun onComplete() {
                }

                override fun onNext(t: DetailBeanResult) {
                    success.value = t
                }
            })
    }

    fun addToCart(body: RequestBody, success: MutableLiveData<AddToCartBeanResult>, failed: MutableLiveData<String>) {
        apiService.addToCart(body)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<AddToCartBeanResult> {
                override fun onSubscribe(d: Disposable) {
                }

                override fun onError(e: Throwable) {
                    failed.value = e.message
                }

                override fun onComplete() {
                }

                override fun onNext(t: AddToCartBeanResult) {
                    success.value = t
                }
            })
    }
}